package com.loe.ui.api

import android.view.ViewGroup
import android.webkit.JavascriptInterface
import com.loe.ui.jsContext
import com.loe.ui.px
import com.loe.ui.view.UiScrollView
import com.quickjs.JSArray
import com.quickjs.JSFunction

class ScrollViewApi(private val view: UiScrollView) : ViewGroupApi(view)
{
    @JavascriptInterface
    override fun removeAllChild()
    {
        (view.getChildAt(0) as ViewGroup).removeAllViews()
    }

    /** 刷新监听 */
    @JavascriptInterface
    fun setOnRefresh(callback: JSFunction)
    {
        view.onRefresh = { callback.call(jsObject, null) }
    }

    /** 加载 */
    @JavascriptInterface
    fun loadData() = view.onRefresh?.invoke()

    /** 显示刷新 */
    @JavascriptInterface
    fun refresh() = view.refresh()

    @JavascriptInterface
    fun setOnScroll(callback: JSFunction)
    {
        view.onScroll = { callback.call(jsObject, JSArray(jsContext).push(it)) }
    }

    @JavascriptInterface
    fun getScrollHeight(): Int
    {
        return view.getScrollHeight()
    }

    @JavascriptInterface
    fun scrollTo(t: String)
    {
        view.scrollTo(0, t.px)
    }

    @JavascriptInterface
    fun smoothScrollTo(t: String)
    {
        view.smoothScrollTo(0, t.px)
    }
}